#include "stm32f10x.h"
#include <OLED.h>
#include "OLED_font.h"
#include "usart_Debug.h"
#include "stdio.h"
void OLED_W_SDA(uint8_t BitVal)
{
	GPIO_WriteBit(GPIOB,  GPIO_Pin_9, (BitAction) BitVal);
}
void OLED_W_SCL(uint8_t BitVal)
{
	GPIO_WriteBit(GPIOB,  GPIO_Pin_8, (BitAction) BitVal);
}

void I2c_init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_OD;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_8;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	OLED_W_SDA(1);
	OLED_W_SCL(1);
}
void I2c_start(void)
{
	OLED_W_SDA(1);
	OLED_W_SCL(1);
	OLED_W_SDA(0);
	OLED_W_SCL(0);
}
void I2c_stop(void)
{
	OLED_W_SDA(0);
	OLED_W_SCL(1);
	OLED_W_SDA(1);
}

void I2c_sendbyte(uint8_t byte)
{
	for(uint8_t i=0;i<8;i++)
	{
		OLED_W_SDA(byte&(0x80>>i));
		OLED_W_SCL(1);
		OLED_W_SCL(0);
	}
	OLED_W_SCL(1);
	OLED_W_SCL(0);
}
void OLED_WriteCommand(uint8_t command)
{
	I2c_start();
	I2c_sendbyte(0x78);
	I2c_sendbyte(0x00);
	I2c_sendbyte(command);
	I2c_stop();
}
void OLED_WriteData(uint8_t data)
{
	I2c_start();
	I2c_sendbyte(0x78);
	I2c_sendbyte(0x40);
	I2c_sendbyte(data);
	I2c_stop();
}
void OLED_SetCursor(uint8_t X,uint8_t Y)
{
	OLED_WriteCommand(0x00|(X&0x0F));
	OLED_WriteCommand(0x10|((X&0xF0)>>4));
	OLED_WriteCommand(0xB0 | Y);
}
void OLED_clear(void)
{
	for(uint8_t i=0;i<8;i++)
	{
		OLED_SetCursor(0,i);
		for(uint8_t j=0;j<128;j++)
		{
			OLED_WriteData(0x00);
		}
	}
}

void OLED_ShowChar(uint8_t Line,uint8_t Column,char val)
{
	OLED_SetCursor((Column-1)*8,(Line-1)*2);
	for(uint8_t i=0;i<8;i++)
	{
		OLED_WriteData(OLED_F8x16[val-' '][i]);
	}
	OLED_SetCursor((Column-1)*8,(Line-1)*2+1);
	for(uint8_t i=0;i<8;i++)
	{
		OLED_WriteData(OLED_F8x16[val-' '][i+8]);
	}
}
uint32_t Pow(uint8_t x,uint8_t Y)
{
	uint32_t i=1;
	while(Y--)
	{
		i=i*x;
	}
	return i;
}
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
	for(uint8_t i=0;i<Length;i++)
	{
		OLED_ShowChar(Line,Column+i,Number/Pow(10,Length-i-1)%10+'0');
	}
}
void OLED_ShowString(uint8_t Line,uint8_t Column,char *string)
{
	for(uint8_t i=0;string[i]!='\0';i++)
	{
		OLED_ShowChar(Line,Column+i,string[i]);
	}
}
void OLED_init(void)
{
	uint32_t i, j;
	
	for (i = 0; i < 1000; i++)			//上电延时
	{
		for (j = 0; j < 1000; j++);
	}
	I2c_init();			//端口初始化
	
	OLED_WriteCommand(0xAE);	//关闭显示
	
	OLED_WriteCommand(0xD5);	//设置显示时钟分频比/振荡器频率
	OLED_WriteCommand(0x80);
	
	OLED_WriteCommand(0xA8);	//设置多路复用率
	OLED_WriteCommand(0x3F);
	
	OLED_WriteCommand(0xD3);	//设置显示偏移
	OLED_WriteCommand(0x00);
	
	OLED_WriteCommand(0x40);	//设置显示开始行
	
	OLED_WriteCommand(0xA1);	//设置左右方向，0xA1正常 0xA0左右反置
	
	OLED_WriteCommand(0xC8);	//设置上下方向，0xC8正常 0xC0上下反置

	OLED_WriteCommand(0xDA);	//设置COM引脚硬件配置
	OLED_WriteCommand(0x12);
	
	OLED_WriteCommand(0x81);	//设置对比度控制
	OLED_WriteCommand(0xCF);

	OLED_WriteCommand(0xD9);	//设置预充电周期
	OLED_WriteCommand(0xF1);

	OLED_WriteCommand(0xDB);	//设置VCOMH取消选择级别
	OLED_WriteCommand(0x30);

	OLED_WriteCommand(0xA4);	//设置整个显示打开/关闭

	OLED_WriteCommand(0xA6);	//设置正常/倒转显示

	OLED_WriteCommand(0x8D);	//设置充电泵
	OLED_WriteCommand(0x14);

	OLED_WriteCommand(0xAF);	//开启显示
		
	OLED_clear();			//OLED清屏
}
void OLED_showchinese(uint8_t Line,uint8_t Column,char*mychinese)
{
	uint8_t mflag=0;
	uint8_t index;
	char singlechinese[4]={0};
	uint8_t i;
	for(i=0;mychinese[i]!='\0';i++)
	{
		singlechinese[mflag]=mychinese[i];
		
		mflag++;
		if(mflag>=3)
		{
			mflag=0;
		
			for(index=0;strcmp(chinese[index].Index," ")!=0;index++)
			{
				if(strcmp(singlechinese,chinese[index].Index)==0)
				{
					
					break;
				}
			}
			OLED_SetCursor((Column+i/3 - 1) * 16,(Line - 1) * 2);
			for (uint8_t j = 0; j < 16; j++)
			{
				OLED_WriteData((uint8_t)chinese[index].data[j]);		//显示下半部分内容
			}
			OLED_SetCursor((Column+i/3 - 1) * 16,(Line - 1) * 2 + 1);	
			for (uint8_t j = 0; j < 16; j++)
			{
				OLED_WriteData((uint8_t)chinese[index].data[j+16]);		//显示下半部分内容
			}
		}
	}
	
}
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
{
	uint8_t i;
	uint32_t Number1;
	if (Number >= 0)
	{
		OLED_ShowChar(Line, Column, '+');
		Number1 = Number;
	}
	else
	{
		OLED_ShowChar(Line, Column, '-');
		Number1 = -Number;
	}
	for (i = 0; i < Length; i++)							
	{
			OLED_ShowChar(Line,Column+i+1,Number1/Pow(10,Length-i-1)%10+'0');
	}
}
